package dataStructure.a_record.singLinkedList;

/**
 * @author lhn
 * @date 2024-09-13 12:44
 */

public class SingleCircleLinkedListDemo {
    public static void main(String[] args) {
        SingleCircleLinkedList singleCircleLinkedList = new SingleCircleLinkedList();
        singleCircleLinkedList.add(new Node_pro(1));
        singleCircleLinkedList.add(new Node_pro(2));
        singleCircleLinkedList.add(new Node_pro(3));
        singleCircleLinkedList.add(new Node_pro(4));
        singleCircleLinkedList.add(new Node_pro(5));
        singleCircleLinkedList.list();
        singleCircleLinkedList.count(2, 3, 5);
    }
}

class SingleCircleLinkedList {
    private Node_pro head = null;
    public void add(Node_pro node) {
        Node_pro temp = head;
        if (temp == null) {
            head = node;
            node.next = node;
            return;
        }
        while (temp != null) {
            if (temp.next == head) {
                temp.next = node;
                node.next = head;
                return;
            }
            temp = temp.next;
        }
    }

    public void list() {
        Node_pro temp = head;
        if (temp == null) {
            System.out.println("链表为空");
            return;
        }
        while (temp != null) {
            if (temp.next == head) {
                System.out.println(temp.no);
                return;
            }
            System.out.println(temp.no);
            temp = temp.next;
        }
    }

    public void count(int startNo, int countNum, int nums) {
        if (startNo < 1 || startNo > nums || countNum < 1 || countNum > nums) {
            System.out.println("输入参数有误，请重新输入");
            return;
        }
        Node_pro node = head;   // 辅助指针目标指向出队列的节点的前一个节点
        // 找到最后节点
        while (true) {
            if (node.next == head) {
                break;
            }
            node = node.next;
        }
        for (int i = 0; i < startNo  - 1; i++) {
            head = head.next;
            node = node.next;   // 找到第几个开始数的前一个节点
        }

        while (true) {
            if (node == head) {
                System.out.println("出队列的节点为：" + head.no);
                System.out.println("出队列完毕");
                break;
            }
            for (int i = 0; i < countNum - 1; i++) {
                head = head.next;
                node = node.next;
            }
            System.out.println("出队列的节点为：" + head.no);
            head = head.next;
            node.next = head;
        }

    }
}

class Node_pro {
    int no;
    Node_pro next;

    public Node_pro() {
    }

    public Node_pro(int no) {
        this.no = no;
    }
}